import Control.Monad
import Data.Array.ST
import qualified Data.Array.Unboxed as U
spfArray :: U.UArray Int Int
spfArray  = runSTUArray (do
  arr <- newArray (0,m-1) 0 
  loop arr 2
  forM_ [2 .. m - 1] $ \ x ->
    loop2 arr x 2
  return arr 
  )
  where
  m=10^6
  loop arr n
      |n>=m=return ()
      |otherwise=do writeArray arr n (n-9*(div (n-1) 9))
                    loop arr (n+1)
  loop2 arr x n 
      |n*x>=m=return ()
      |otherwise=do incArray arr x n
                    loop2 arr x (n+1)
  incArray arr x n = do
      a <- readArray arr x
      b <- readArray arr n
      ab <- readArray arr (x*n)
      when(ab<a+b) (writeArray arr (x*n) (a + b))
writ x=appendFile "p159.log"$foldl (++) "" [show x,"\n"]
main=do
    mapM_ writ $U.elems spfArray
problem_159 = main